package findzip

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"encoding/base64"
	"strings"
)

// var _key = []int{-67, -122, 116, -85, -50, 73, 15, -95, -34, 0, -84, -113, -110, 24, 105, 80}
// var _key = []int{48, -44, 80, {-67, -122, 116, -85, -50, 73, 15, -95, -34, 0, -84, -113, -110, 24, 105, 80}44, 107, 23, -101, 71, -92, 86, 124, -96, 61, 4, -49, 24}
// var _key = []int{40, -13, -126, 115, -108, 34, 33, 122, 40, -93, 83, 45, 50, 68, 65, 113}
// 618672
// var key = []byte{40, 243, 130, 115, 148, 34, 33, 122, 40, 163, 83, 45, 50, 68, 65, 113}

// 718915
// [-67, -122, 116, -85, -50, 73, 15, -95, -34, 0, -84, -113, -110, 24, 105, 80]
//              [75, 32, 17, -125,-113, 35,-116, 2,-10, 18, -103,-39,-123, -90,-122, -122]
// var key = []byte{75, 32, 17, 131, 143, 35, 140, 2, 246, 18, 153, 217, 133, 166, 134, 134}
// [-90, 41, 73, 15, -117, 55, -14, -128, 58, 19, 37, -80, -59, -95, 51, 104]
// [68, -85, -52, 23, 49, -128, -125, 115, -70, -111, -40, -26, 34, -6, -62, 29]

//              [-19, 102, -85,  -87, -61, 120, -42, 28, 12, 2, -96, -60, -42, -76, -122, -39]
// var key = []byte{237, 102, 171, 169, 195, 120, 214, 28, 12, 2, 160, 196, 214, 180, 134, 217}

// [105, 23, 110, 48, 102, 38, 112, -72, 9, -114, -36, -116, -92, -79, -68, 77]
var key = []byte{105, 23, 110, 48, 102, 38, 112, 184, 9, 142, 220, 140, 164, 177, 188, 77}

//  [-35, 75, 32, -113, -85, 57, -125, 89, -53, 120, 115, -89, -43, 111, -86, 42]

func Encode(text string) string {
	data := []byte(text)

	// key, err := base64.StdEncoding.DecodeString(password)
	// if err != nil {
	// 	panic(err)
	// }
	// key = md5.New().Sum(key)

	aesBlockEncrypter, err := aes.NewCipher(key)
	content := PKCS5Padding(data, aesBlockEncrypter.BlockSize())
	encrypted := make([]byte, len(content))
	if err != nil {
		panic(err)
	}
	aesEncrypter := cipher.NewCBCEncrypter(aesBlockEncrypter, key)
	aesEncrypter.CryptBlocks(encrypted, content)
	return base64.StdEncoding.EncodeToString(encrypted)
}

func Decrypt(text string) string {
	ts := strings.Split(text, "|")
	if len(ts) == 1 {
		return text
	}
	t := ts[1]

	src, err := base64.StdEncoding.DecodeString(t)
	if err != nil {
		panic(err)
	}

	var aesBlockDecrypter cipher.Block
	aesBlockDecrypter, err = aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	decrypted := make([]byte, len(src))

	aesDecrypter := cipher.NewCBCDecrypter(aesBlockDecrypter, key[:16])
	aesDecrypter.CryptBlocks(decrypted, src)
	return string(PKCS5Trimming(decrypted))
}

var title = []string{"receiver_address", "receiver_name", "receiver_mobile"}

func ToText(name, text string) string {
	for _, t := range title {
		if name == t {
			return Decrypt(text)
		}
	}

	return text
}

func PKCS5Padding(cipherText []byte, blockSize int) []byte {
	padding := blockSize - len(cipherText)%blockSize
	padText := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(cipherText, padText...)
}

func PKCS5Trimming(encrypt []byte) []byte {
	padding := encrypt[len(encrypt)-1]
	return encrypt[:len(encrypt)-int(padding)]
}
